package cn.itcast.bos.service.Impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.IDecidedzoneDao;
import cn.itcast.bos.dao.base.INoticebillDao;
import cn.itcast.bos.dao.base.IWorkbillDao;
import cn.itcast.bos.domain.Decidedzone;
import cn.itcast.bos.domain.Noticebill;
import cn.itcast.bos.domain.Staff;
import cn.itcast.bos.domain.Workbill;
import cn.itcast.bos.domain.pagebean.PageBean;
import cn.itcast.bos.service.INoticebillService;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class NoticebillServiceImpl implements INoticebillService{
	@Resource
	private INoticebillDao noticebillDao;
	@Resource
	private CustomerService  customerService;
	@Resource
	private IDecidedzoneDao  decidedzoneDao;
	@Resource
	private IWorkbillDao WorkbillDao;

	@Override
	public void save(Noticebill model) {
		noticebillDao.save(model); // 获得持久对象Noticebill
		//获得 定区id 
		String decidedzoneid = customerService.findByAddress(model.getPickaddress());
		//通过定区id  关联取派员
		if (decidedzoneid !=null) {
			Decidedzone decidedzone = decidedzoneDao.findById(decidedzoneid);
			Staff staff = decidedzone.getStaff();
			model.setStaff(staff);
			model.setOrdertype("自动");
			
			//为取派员创建一个工单
			Workbill workbill = new Workbill();
			workbill.setNoticebill(model);
			workbill.setStaff(staff);
			workbill.setBuildtime(new Timestamp(System.currentTimeMillis()));
			workbill.setAttachbilltimes(0);
			workbill.setPickstate("未取件");
			workbill.setType("新");
			workbill.setRemark(model.getRemark());
			WorkbillDao.save(workbill);
		}else {
			model.setOrdertype("手动");
		}
		
	}

	@Override
	public void pageQueryList(PageBean pageBean) {
		noticebillDao.pageQueryList(pageBean);
	}

	
}
